/**
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.api.adwords.starterkit.wrappers;

import com.google.api.adwords.starterkit.db.keys.KeywordParamKey;
import com.google.api.adwords.starterkit.db.structures.KeywordParamStructure;
import com.google.api.adwords.starterkit.db.tables.KeywordParamTable;

import java.sql.Connection;

/**
 * The Class KeywordParamWrapper manages all synchronization process related to
 * KeywordParam.
 * 
 * @author rodrigo.f.vale@gmail.com
 */
public class KeywordParamWrapper {
  private KeywordParamStructure kwdParamStruct = null;
  private KeywordParamTable keywordParamTable;

  /**
   * Instantiates a new keyword param wrapper.
   * 
   * @param kwdWrapper the kwd wrapper
   * @param param1 the param1
   * @param param2 the param2
   * @param conn the conn
   */
  public KeywordParamWrapper(KeywordWrapper kwdWrapper, String param1,
      String param2, Connection conn) {
    kwdParamStruct = new KeywordParamStructure();

    kwdParamStruct.setLocalAdGroupId(kwdWrapper.getLocalAdGroupId());
    kwdParamStruct.setLocalId(kwdWrapper.getLocalId());
    kwdParamStruct.setParam1(param1);
    kwdParamStruct.setParam2(param2);
    kwdParamStruct.setUpdated(true);

    keywordParamTable = new KeywordParamTable(conn);

    KeywordParamKey kwdParamKey =
        new KeywordParamKey(kwdParamStruct.getLocalId(),
            kwdParamStruct.getLocalAdGroupId());
    if (keywordParamTable.exists(kwdParamKey)) {
      KeywordParamStructure kwdParamStructDB =
          keywordParamTable.get(kwdParamKey);
      boolean modified = false;
      if (!kwdParamStruct.getParam1().equals(kwdParamStructDB.getParam1())) {
        kwdParamStructDB.setParam1(kwdParamStruct.getParam1());
        modified = true;
      }
      if (!kwdParamStruct.getParam2().equals(kwdParamStructDB.getParam2())) {
        kwdParamStructDB.setParam2(kwdParamStruct.getParam2());
        modified = true;
      }
      if (modified) {
        kwdParamStruct = keywordParamTable.save(kwdParamStructDB, true);
      }
    } else {
      kwdParamStruct = keywordParamTable.save(kwdParamStruct, true);
    }
  }

  public String getParam1() {
    return kwdParamStruct.getParam1();
  }

  public String getParam2() {
    return kwdParamStruct.getParam2();
  }

  public void setParam1(String param1) {
    kwdParamStruct.setParam1(param1);
    kwdParamStruct = keywordParamTable.save(kwdParamStruct, true);
  }

  public void setParam2(String param2) {
    kwdParamStruct.setParam2(param2);
    kwdParamStruct = keywordParamTable.save(kwdParamStruct, true);
  }
}